<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class InternalMail 
{
	public $to;
	public $cc;
	public $from;
	public $body;
	public $subject;
	public $icon;

	private $to_keys;
	private $cc_keys;

	private $checker;
	private $alerter;
	private $db_access;
	private $error_nav;
	private $address_translator;

	public function __construct() {
		$this->checker = new InputChecker();
		$this->alerter = new Alerter();
		$this->db_access = new DbAccessor();
		$this->error_nav = new ErrorNavigation();
		$this->address_translator = new AddressTranslator();
	}

	public function set_alerter($alerter) {
		$this->alerter = $alerter;
	}

	public function set_db_accessor($accessor) {
		$this->db_access = $accessor;
	}

	public function set_error_navigation($navigation) {
		$this->error_nav = $navigation;
	}

	public function set_input_checker($checker) {
		$this->checker = $checker;
	}

	public function set_address_translator($address_translator) {
		$this->address_translator = $address_translator;
	}

	public function get_to_addresses() {
		return $this->to;
	}

	public function get_cc_addresses() {
		return $this->cc;
	}

	public function set_to_keys($value) {
		$this->to_keys = $value;
	}

	public function set_cc_keys($value) {
		$this->cc_keys = $value;
	}

	public function send_mail($keyed_addresses = 0) {
		try {
			if (!$this->from) {
				$this->error_nav->errorpage(9);
			}
			if (!$this->to) {
				throw new MailException('Je selecteerde geen leden om de mail naar te sturen.');
			}
			$this->prepare_for_sending($keyed_addresses);
			$id = $this->insert_mail_header();
			$this->mail_all($this->to_keys, $id);
			$query = $this->db_access->db_query_insert(array('mail_id', 'body'), array($id, $this->db_access->db_sqlize($this->body)), "MAIL_BODY");
			$this->db_access->db_do_query($query);
			if ($this->cc_keys and (is_array($this->cc_keys) and count($this->cc_keys))) { 
				$this->mail_all($this->cc_keys, $id);
			}
			$_POST['mailing'] = 0;
		} catch (MailException $ex) {
			$this->alerter->add_alert($ex->getMessage());
		} catch (Exception $ex) {
			$this->alerter->add_alert("Er deed zich een onverwachte fout voor.");
			$this->alerter->add_alert("Mail werd niet verzonden");
		}
	}

	private function insert_mail_header() {

		$sqlized_icon = $this->db_access->db_sqlize($this->icon);
		$sqlized_subject = $this->db_access->db_sqlize($this->subject);
		$insert_query = $this->db_access->db_query_insert(array('sender', 'subject', 'sent', 'hasread', 'icon'), 
														  array($this->from, $sqlized_subject, 'NOW(14)', 1, $sqlized_icon), "MAIL_HEADER");
		$this->db_access->db_do_query($insert_query);
		return $this->db_access->db_insert_id();
	}

	private function mail_all($keys, $id) {
		$insert = array();
		while ($t = array_pop($keys)) {
			$insert[] = array($t, $id);
		}
		$query = $this->db_access->db_query_insert_multiple(array('user_id', 'mail_id'), $insert, "MAIL_TO", 1);
		$this->db_access->db_do_query($query);
	}

	private function prepare_for_sending($keyed_addresses) {
		$this->set_to_keys($this->address_translator->translate_addresses_to_db_keys($keyed_addresses, $this));
		if(!$this->checker->check_mail_input($this)) {
			throw new MailException("De tekst kon niet gevalideerd worden.");
		}
	}
}

class MailException extends Exception {
}

?>
